package com.chookss.mine.system;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.chookss.R;
import com.chookss.Urls;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.OkGoUtil;
import com.chookss.tools.okgoconfig.callback.JsonCallback;
import com.chookss.view.InitDialog;
import com.chookss.view.listener.ShareDialogListener;
import com.lvgroup.hospital.base.BaseAct;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyNoticeActivity extends BaseAct {
    @BindView(R.id.tvIntegralContent)
    TextView tvIntegralContent;
    @BindView(R.id.common_title_txt)
    TextView common_title_txt;
    @BindView(R.id.common_right_txt)
    TextView common_right_txt;
    @BindView(R.id.tvIntegralTime)
    TextView tvIntegralTime;
    @BindView(R.id.tvFansContent)
    TextView tvFansContent;
    @BindView(R.id.tvFansTime)
    TextView tvFansTime;
    @BindView(R.id.tvPraiseContent)
    TextView tvPraiseContent;
    @BindView(R.id.tvPraiseTime)
    TextView tvPraiseTime;
    @BindView(R.id.tvCommentsContent)
    TextView tvCommentsContent;
    @BindView(R.id.tvCommentsTime)
    TextView tvCommentsTime;
    @BindView(R.id.tvSystemContent)
    TextView tvSystemContent;
    @BindView(R.id.tvSystemTime)
    TextView tvSystemTime;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        setContentView(R.layout.activity_my_notice_list);
        ButterKnife.bind(this);
        initState();
    }

    @Override
    public void initData() {
        common_title_txt.setText("通知");
//        common_right_txt.setText("清除未读消息");
//        common_right_txt.setTextColor(getResources().getColor(R.color.main_color));
//        common_right_txt.setVisibility(View.VISIBLE);
        getData();
    }

    private void getData() {
        MyHttpRequest.postRequest(Urls.getMyMessageList, this, SignUtils2.makeSignStr(null), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if ("200".equals(jsonObject.getString("code"))) {
                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                        if (tvIntegralContent == null) {
                            return;
                        }
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                            String messageType = jsonObject1.getString("messageType");
                            String messageContent = jsonObject1.getString("messageContent");
                            String messageTitle = jsonObject1.getString("messageTitle");
                            String createTime = jsonObject1.getString("createTime");
                            if ("0".equals(messageType)) {
                                tvIntegralContent.setText(messageTitle + "  " + messageContent);
                                tvIntegralTime.setText(Utils.getAccurateDate(createTime));
                            } else if ("1".equals(messageType)) {
                                tvFansContent.setText(messageContent);
                                tvFansTime.setText(Utils.getAccurateDate(createTime));
                            } else if ("2".equals(messageType)) {
                                tvPraiseContent.setText(messageContent);
                                tvPraiseTime.setText(Utils.getAccurateDate(createTime));
                            } else if ("3".equals(messageType)) {
                                tvCommentsContent.setText(messageContent);
                                tvCommentsTime.setText(Utils.getAccurateDate(createTime));
                            } else if ("4".equals(messageType)) {
                                tvSystemContent.setText(messageContent);
                                tvSystemTime.setText(Utils.getAccurateDate(createTime));
                            }
                        }

                    } else {
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"), jsonObject.getString("msg"));
                        } else {
                            MyToast.show(jsonObject.getString("msg"));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void clearData() {
        MyHttpRequest.postRequest(Urls.deleteWDMessageCount, this, SignUtils2.makeSignStr(null), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
            }
        });
    }

    @OnClick({R.id.common_title_back, R.id.common_right_txt, R.id.llIntegral, R.id.llFans, R.id.llPraise, R.id.llComments, R.id.llSystem})
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.common_right_txt:
                new InitDialog(this,
                        "提示", "确认要清除未读消息吗?", "确认", true,
                        new ShareDialogListener() {
                            @Override
                            public void onClick() {
                                clearData();
                            }
                        }).show();
                break;
            case R.id.llIntegral:
                intent = new Intent(this, MyIntegralActivity.class);
                intent.putExtra("messageType", 0);
                startActivity(intent);
                break;
            case R.id.llFans:
                intent = new Intent(this, MyPraiseActivity.class);
                intent.putExtra("messageType", 1);
                startActivity(intent);
                break;
            case R.id.llPraise:
                intent = new Intent(this, MyPraiseActivity.class);
                intent.putExtra("messageType", 2);
                startActivity(intent);
                break;
            case R.id.llComments:
                intent = new Intent(this, MyPraiseActivity.class);
                intent.putExtra("messageType", 3);
                startActivity(intent);
                break;
            case R.id.llSystem:
                intent = new Intent(this, MyIntegralActivity.class);
                intent.putExtra("messageType", 4);
                startActivity(intent);
                break;
        }
    }
}
